package edu.ihs.lhpy.service.impl;

import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import edu.ihs.lhpy.base.BaseDaoImpl;
import edu.ihs.lhpy.cfg.Configuration;
import edu.ihs.lhpy.domain.PageBean;
import edu.ihs.lhpy.domain.User;
import edu.ihs.lhpy.service.UserService;

@Service
public class UserServiceImpl extends BaseDaoImpl<User> implements UserService{

	@Override
	public User getByLoginNameAndPassword(String name, String password) {
		return (User)getSession().createQuery(//
				" FROM User u WHERE u.name=? AND u.password=?")//
				.setParameter(0, name)//
				.setParameter(1, DigestUtils.md5Hex(password))// 要使用MD5的摘要
				.uniqueResult();
	}

	@Override
	public PageBean getPageBean(int pageNum) {
		int pageSize = Configuration.getPageSize();
		//查询本页的数据列表
		List recordList = getSession().createQuery(//
				"FROM User n ORDER BY id ")//
				.setFirstResult((pageNum - 1) * pageSize)
				.setMaxResults(pageSize)
				.list();
		//查询总列表数
		Long count = (Long)getSession().createQuery(//
				" SELECT COUNT(*) FROM User ")//
				.uniqueResult();
		
		return new PageBean(recordList, pageSize, pageNum, count.intValue());
	}
	
	
}
